package com.longcai.controller.clyjuser;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.longcai.bean.ApiResult;
import com.longcai.bean.PageHelper;
import com.longcai.entity.customer.ClyjGift;
import com.longcai.entity.customer.ClyjUser;
import com.longcai.entity.homegood.ClyjGoodsHomepage;
import com.longcai.entity.homegood.ClyjGoodsPics;
import com.longcai.entity.homepage.ClyjGoodsType;
import com.longcai.page.table.PageTableResponse;
import com.longcai.service.clyjuser.ClyjGiftService;
import com.longcai.service.goods.ClyjGoodsHomepageService;
import com.longcai.util.OSSUtils;
import com.longcai.util.listUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 礼包表 前端控制器
 * </p>
 *
 * @author M
 * @since 2019-04-18
 */
@RestController
@RequestMapping("/clyjGift")
public class ClyjGiftController {

    @Autowired
    private ClyjGiftService clyjGiftService;
    @Autowired
    private ClyjGoodsHomepageService goodsService;
    

    /**
     * 获取礼包列表
     *
     * @param clyjGift
     * @param pageHelper
     * @return
     */
    @ApiOperation(value = "礼包列表")
    @GetMapping(value = "/giftList")
    public PageTableResponse giftList(ClyjGift clyjGift, PageHelper pageHelper) {

        return this.clyjGiftService.giftList(clyjGift, pageHelper);
    }
    
    /**
     * 获取商品列表
     *
     * @return
     */
    @ApiOperation(value = "商品列表")
    @PostMapping(value = "/getGoods")
    public ApiResult getGoods() {
        try {
        	return goodsService.getGoodsByType("100030");
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResult.fail("查询失败"+e.getMessage());
        }
    }


    /**
     * 根据id获取礼包信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id获取礼包信息")
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('gift:query')")
    public ClyjGift get(@PathVariable String id) {
        ClyjGift clyjGift = clyjGiftService.selectById(id);
        return clyjGift;
    }


    /**
     * 修改礼包
     *
     * @param clyjGift
     * @return
     */
    @PostMapping(value = "/updategift")
    @PreAuthorize("hasAuthority('gift:update')")
    public Integer updateUser(ClyjGift clyjGift, MultipartFile code1, MultipartFile picBanner1, MultipartFile[] picLb1, MultipartFile picBig1) {
        if (code1 != null) {
            //文档上传oss
            try {
                String fileName = code1.getOriginalFilename();
                String pox = fileName.substring(fileName.lastIndexOf(".") + 1);
                InputStream inputStream = code1.getInputStream();
                Map<String, Object> uploadByInputStream = OSSUtils.uploadByInputStream(inputStream, pox, "CLYJ/GIFT/");
                clyjGift.setCode(uploadByInputStream.get("path").toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (picBanner1 != null) {
            //文档上传oss
            try {
                String fileName = picBanner1.getOriginalFilename();
                String pox = fileName.substring(fileName.lastIndexOf(".") + 1);
                InputStream inputStream = picBanner1.getInputStream();
                Map<String, Object> uploadByInputStream = OSSUtils.uploadByInputStream(inputStream, pox, "CLYJ/GIFT/");
                clyjGift.setPicBanner(uploadByInputStream.get("path").toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (picBig1 != null) {
            //文档上传oss
            try {
                String fileName = picBig1.getOriginalFilename();
                String pox = fileName.substring(fileName.lastIndexOf(".") + 1);
                InputStream inputStream = picBig1.getInputStream();
                Map<String, Object> uploadByInputStream = OSSUtils.uploadByInputStream(inputStream, pox, "CLYJ/GIFT/");
                clyjGift.setPicBig(uploadByInputStream.get("path").toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        List list = new ArrayList();
        if (picLb1 != null) {
            //文档上传oss
            try {
                for (MultipartFile multipartFile : picLb1) {
                    //上传文件
                    String fileName = multipartFile.getOriginalFilename();
                    String pox = fileName.substring(fileName.lastIndexOf(".") + 1);
                    InputStream inputStream = multipartFile.getInputStream();
                    Map<String, Object> uploadByInputStream = OSSUtils.uploadByInputStream(inputStream, pox, "CLYJ/GIFT/");
                    list.add(uploadByInputStream.get("path").toString());
                    String s = listUtil.listToString(list);
                    clyjGift.setPicLb(s);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //判断id
        if (clyjGift.getId() == null || clyjGift.getId().equals("")) {
            return this.clyjGiftService.saveGift(clyjGift);
        } else {
            return this.clyjGiftService.updategift(clyjGift);
        }
    }

    /**
     * 删除礼包
     *
     * @param id
     */
    @PostMapping("{id}")
    @ApiOperation(value = "删除礼包")
    @PreAuthorize("hasAuthority('gift:delete')")
    public void delGift(@PathVariable String id) {
        this.clyjGiftService.delGift(id);
    }

}

